package com.ttg.web.service.job.primitive.impl;

import com.google.common.collect.Lists;
import com.google.common.util.concurrent.ListenableFuture;
import com.ttg.common.annotation.TaskRunMethod;
import com.ttg.common.dto.PrimitiveBaseDTO;
import com.ttg.common.utils.DateTimeUtil;
import com.ttg.common.vo.TaskResultVO;
import com.ttg.model.pojo.BankOrderCreditInfoPrimitive;
import com.ttg.model.vo.OrderCreditInfoPrimitiveTaskVO;
import com.ttg.web.dao.BankOrderCreditInfoPrimitiveDao;
import com.ttg.web.juc.TasktemplateHelper;
import com.ttg.web.service.job.JobCommon;
import com.ttg.web.service.job.primitive.OrderCreditInfoPrimitiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StopWatch;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

/**
 * Description：
 * Author: chenyou
 * Date: 2025/11/12 14:00
 * Company: ttg
 * Copyright: Copyright (c) 2025
 */
@Service
@TaskRunMethod(value = "saveTradeCreditInfoPrimitiveTask")
public class OrderCreditInfoPrimitiveServiceImpl extends JobCommon implements OrderCreditInfoPrimitiveService {
    @Value("${tlinx.isJuc:false}")
    private volatile Boolean isJuc;//是否启用异步回调
    @Autowired
    private TasktemplateHelper tasktemplateHelper;
    @Autowired
    private BankOrderCreditInfoPrimitiveDao bankOrderCreditInfoPrimitiveDao;

    @Override
    public TaskResultVO saveTradeCreditInfoPrimitive(PrimitiveBaseDTO reqDto) {
        StopWatch sw = new StopWatch();
        sw.start();
        LocalDate date = DateTimeUtil.parseYYYYMMDD(reqDto.getDate());
        //每日定时从晋享e付同步商户交易信贷数据
        //todo sftp拉取过程
        List<BankOrderCreditInfoPrimitive> list = getOrderCreditInfoList(LocalDateTime.of(date, LocalTime.MIN));

        //删除指定日期的旧数据
        bankOrderCreditInfoPrimitiveDao.deleteByDay(date);

        //拆分,按批次执行
        List<ListenableFuture<TaskResultVO>> futures = new ArrayList<>();
        List<TaskResultVO> results = new ArrayList<>();
        List<List<BankOrderCreditInfoPrimitive>> partLists = Lists.partition(list,500);
        for(List<BankOrderCreditInfoPrimitive> part:partLists){
            OrderCreditInfoPrimitiveTaskVO vo = new OrderCreditInfoPrimitiveTaskVO();
            vo.setPart(part);
            if(isJuc){
                futures.add(tasktemplateHelper.submit(this,vo));
            }else {
                TaskResultVO result = saveTradeCreditInfoPrimitiveTask(vo);
                results.add(result);
            }
        }
        sw.stop();
        System.out.println("耗时：" + sw.getTotalTimeSeconds() + "s");
        return taskResult(results,futures,isJuc);
    }

    public TaskResultVO saveTradeCreditInfoPrimitiveTask(OrderCreditInfoPrimitiveTaskVO vo){
        //todo 梳理数据
        List<BankOrderCreditInfoPrimitive> part = vo.getPart();
        //保存数据
        bankOrderCreditInfoPrimitiveDao.saveBatch(part);
        return taskReuslt(JobCommon.Status.SUCCESS.getValue(),false,"ok");
    }
}
